package com.it.commicity.service.impl.sys;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.it.commicity.domain.po.SysMenu;
import com.it.commicity.domain.po.SysRoleMenu;
import com.it.commicity.mapper.sys.SysMenuMapper;
import com.it.commicity.service.sys.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.commicity.service.sys.ISysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-07-11
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {
    @Autowired
    private ISysRoleMenuService roleMenuService;
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private SysMenuMapper menuMapper;

    @Override
    public List<SysMenu> getMenuTree(Long userId) {
        //根据用户id,查找role_id,再查找 Menu
        List<SysRoleMenu> sysRoleMenus = roleMenuService.selectRoleMenuByUserId(userId);
        ArrayList<Long> list = new ArrayList<>();
        //取出所有的MenuId
        sysRoleMenus.forEach(menu -> {
            list.add(menu.getMenuId());
        });
        //查询parent_id为零的菜单目录
        List<SysMenu> menusBase = menuService.selectMenusParentId(0);
        //查询parent_id不为零的菜单目录
        List<SysMenu> menuNotBase = menuService.selectAllNotBase();
        //获取当前用的所属的菜单
        menusBase = getMenuByList(list, menusBase);
        menuNotBase = getMenuByList(list, menuNotBase);
        //装入为零的菜单目录的 显示 菜单
        List<SysMenu> SysMenuList = new ArrayList<>();
        //menuNotBase的getVisible 为 1 的舍去 SysMenuList
        for (SysMenu m : menuNotBase) {
            //0:显示 1:隐藏
            if ("0".equals(m.getVisible())) {
                SysMenuList.add(m);
            }
        }
        //遍历为零的菜单目录,并为菜单为零的(menusBase) 添加Children
        for (SysMenu menu : menusBase) {
            List<SysMenu> menus = iterateMenus(SysMenuList, menu.getMenuId());
            if ("0".equals(menu.getVisible())) {
                menu.setChildren(menus);
            }
        }
        //去除隐藏的parent_id菜单目录
        List<SysMenu> list1 = new ArrayList<>();
        for (SysMenu m : menusBase) {
            if ("0".equals(m.getVisible())) {
                list1.add(m);
            }
        }
//        System.out.println(list1);
        return list1;
    }

    //获取当前用的所属的菜单
    private List<SysMenu> getMenuByList(ArrayList<Long> list, List<SysMenu> menusBase) {
        ArrayList<SysMenu> userList = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < menusBase.size(); j++) {
                if (list.get(i).equals(menusBase.get(j).getMenuId())) {
                    userList.add(menusBase.get(j));
                }
            }
        }
        return userList;
    }
    //多级菜单查询方法
    //根据menu_id

    /**
     * 获取parent_id为零的menu_Id,找到对应的SysMenuList中的集合元素
     *
     * @param SysMenuList 不为零的Menu集合
     * @param menuId      parent_id为零的menu_Id
     * @return
     */
    private List<SysMenu> iterateMenus(List<SysMenu> SysMenuList, Long menuId) {
        ArrayList<SysMenu> result = new ArrayList<>();
        for (SysMenu menu : SysMenuList) {
            //获取菜单的menu_id
            String menuId1 = String.valueOf(menu.getMenuId());
            //获取菜单的父parent_id
            String parentId1 = String.valueOf(menu.getParentId());
            //当Str为空白或者null时，isNotBlank返回false
            //当Str的length>0时，isNotBlank返回true
            if (StringUtils.isNotBlank(parentId1)) {
                //将非零的菜单父id和传进来的父级id比较
                if (parentId1.equals(String.valueOf(menuId))) {
                    //递归查询当前子菜单的子菜单
                    List<SysMenu> iterateMenu = iterateMenus(SysMenuList, Long.valueOf(menuId1));
                    menu.setChildren(iterateMenu);
                    result.add(menu);
                }
            }
        }
//        System.out.println("chirld=" + result);
        return result;
    }

    //根据菜单parent_id 查询所有菜单
    @Override
    public List<SysMenu> selectMenusParentId(int parentId) {
        return menuMapper.selectByMenuId(parentId);
    }

    @Override
    public List<SysMenu> selectAllNotBase() {
        return menuMapper.selectAllNotBase();
    }
}
